package com.mc.servlet;

import com.mc.bean.Match;
import com.mc.bean.MatchState;
import com.mc.bean.MatchType;
import com.mc.service.MatchService;
import com.mc.service.MatchStateService;
import com.mc.service.MatchTypeService;
import com.mc.service.impl.MatchServiceImpl;
import com.mc.service.impl.MatchStateServiceImpl;
import com.mc.service.impl.MatchTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author zl
 * 添加赛事控制器
 */
@WebServlet("/addmatch.action")
public class AddMatchServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建业务逻辑实现
        MatchService matchService = new MatchServiceImpl();
        MatchStateService matchStateService=new MatchStateServiceImpl();
        MatchTypeService matchTypeService=new MatchTypeServiceImpl();
        //接收type判断第几次进来
        String type=req.getParameter("type");
        //如果第一次进来
        if(type.equals("1")){
            //因为有两个字段用来选择赛事状态和赛事类型的
            //要把这两个集合传到前端页面
            List<MatchState> matchStateList=matchStateService.findAll();
            List<MatchType> matchTypeList=matchTypeService.findAll();
            req.setAttribute("matchstatelist",matchStateList);
            req.setAttribute("matchtypelist",matchTypeList);
            //转发到前台增加页面
            req.getRequestDispatcher("addmatch.jsp").forward(req,resp);
        } else if (type.equals("2")) {
            //第二次进来要获取前端传来的要添加的数据
            String name = req.getParameter("name");
            String overview = req.getParameter("about");
            String starttime = req.getParameter("starttime");
            starttime=starttime.replace("T"," ");
            String endtime = req.getParameter("endtime");
            endtime=endtime.replace("T"," ");
            String msid = req.getParameter("msid");
            String mtid = req.getParameter("mtid");
            System.out.println(starttime);
            System.out.println(endtime);
            try {
                Match match = new Match(name,overview,new Timestamp(new SimpleDateFormat("yyyy-MM-dd hh:mm").parse(starttime).getTime()),
                        new Timestamp(new SimpleDateFormat("yyyy-MM-dd hh:mm").parse(endtime).getTime()),Integer.parseInt(msid),Integer.parseInt(mtid));
                System.out.println(match);
                boolean flag = matchService.add(match);
                System.out.println(flag);
                resp.sendRedirect("matchlist.action");
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
